package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.TextView;

import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.util.CalendarUtil;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.calendar.CalendarLayout;

public final class CalendarDialog extends BaseDialog implements CalendarLayout.OnCalendarChangeListener {

    private OnCalendarChangeListener listener;

    private int selectYear = CalendarUtil.getYear();
    private int selectMonth = CalendarUtil.getMonth();
    private int selectDay = CalendarUtil.getDay();

    private int curYear, curMonth;

    public CalendarDialog(Activity activity) {
        super(activity);
    }

//    public CalendarDialog(Activity activity, int year, int month, int day) {
//        super(activity);
//    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_calendar;
    }

    @Override
    protected void initData() {

        final CalendarLayout calendar = findViewById(R.id.dialog_calendar_list);

        findViewById(R.id.dialog_calendar_preview).setOnClickListener(v -> {
            calendar.scrollTo(curYear, curMonth - 1);
        });
        findViewById(R.id.dialog_calendar_next).setOnClickListener(v -> {
            calendar.scrollTo(curYear, curMonth + 1);
        });
    }

    @Override
    public void show() {
        super.show();

        final CalendarLayout calendar = findViewById(R.id.dialog_calendar_list);
        calendar.setOnCalendarChangeListener(this);

        final int year = CalendarUtil.getYear();
        setRange(year - 10, year);
        setScroll(selectYear, selectMonth, selectDay);
    }

    @Override
    public void onCalendarChange(int year, int month, int day, int maxDay, boolean isClick, boolean isFirst) {
        LogUtil.e("onCalendarChange", "year = " + year + ", month = " + month + ", day = " + day + ", maxDay = " + maxDay + ", isClick = " + isClick + ", isFirst = " + isFirst);

        curYear = year;
        curMonth = month;

        final String months = String.valueOf(month < 10 ? "0" + month : month);
        final String days = String.valueOf(day < 10 ? "0" + day : day);
        final String maxDays = String.valueOf(maxDay < 10 ? "0" + maxDay : maxDay);

        final TextView text = findViewById(R.id.dialog_calendar_date);
        if (null != text) {
            text.setText(year + "-" + months);
        }

        if (null != listener) {
            listener.onCalendarChange(String.valueOf(year), months, days, maxDays, isClick);
        }

        if (isClick) dismiss();
    }

    public final void setScroll(String yearmonthday) {

        if (TextUtils.isEmpty(yearmonthday))
            return;

        final String[] split = yearmonthday.split("-");
        if (null == split || split.length != 3)
            return;

        selectYear = Integer.parseInt(split[0]);
        selectMonth = Integer.parseInt(split[1]);
        selectDay = Integer.parseInt(split[2]);
    }

    public final void setScroll(int year, int month, int day) {
        final CalendarLayout calendar = findViewById(R.id.dialog_calendar_list);
        calendar.notifyDataSetChanged(year, month, day);
    }

    public final void setRange(int minYear, int maxYear) {
        final CalendarLayout calendar = findViewById(R.id.dialog_calendar_list);
        calendar.setRange(minYear, 1, maxYear, CalendarUtil.getMonth());
    }

    public final CalendarLayout getCalendarView() {
        final CalendarLayout calendar = findViewById(R.id.dialog_calendar_list);
        return calendar;
    }

    public void setOnCalendarChangeListener(OnCalendarChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnCalendarChangeListener {
        void onCalendarChange(String years, String months, String day, String maxDay, boolean isClick);
    }
}
